package com.hzh.page.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzh.page.common.exception.ResponseEnum;
import com.hzh.page.common.utils.JwtUtils;
import com.hzh.page.common.utils.MD5;
import com.hzh.page.common.utils.R;
import com.hzh.page.pojo.AuthenticationUser;
import com.hzh.page.pojo.UserLoginRecord;
import com.hzh.page.service.AuthenticationUserService;
import com.hzh.page.service.UserLoginRecordService;
import com.hzh.page.vo.AuthenticationUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "登录验证")
@RestController
@RequestMapping(value = "/page/admin")
public class LoginController {
    @Autowired
    private AuthenticationUserService userService;

    @Autowired
    private UserLoginRecordService userLoginRecordService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R login(@RequestBody AuthenticationUser user, HttpServletRequest request) {
        AuthenticationUser userInfo = userService.getOne(new QueryWrapper<AuthenticationUser>().eq("user_name", user.getUserName()));
        if (userInfo == null) {
            return R.error(ResponseEnum.LOGIN_MOBILE_ERROR.getMessage());
        }
        if (!MD5.encrypt(user.getPassWord()).equals(userInfo.getPassWord())) {
            return R.error("密码错误，请重新输入");
        }
        if (!userInfo.getRoleState()) {
            return R.error("该用户已停用");
        } else {
            AuthenticationUserVO userVO = userService.getUserInfoById(userInfo.getId());
            String token = JwtUtils.createToken(userInfo.getId(), userInfo.getUserName());
            userVO.setToken(token);
            UserLoginRecord userLoginRecord = new UserLoginRecord();
            userLoginRecord.setIp(request.getRemoteAddr());
            userLoginRecord.setUserId(userInfo.getId());
            userLoginRecordService.save(userLoginRecord);
            return R.ok().put("userVo", userVO);
        }
    }

    @ApiOperation("校验令牌")
    @PostMapping("/checkToken")
    public R checkToken(HttpServletRequest request) {

        String token = request.getHeader("token");
        boolean result = JwtUtils.checkToken(token);

        if (result) {
            return R.ok();
        } else {
            //LOGIN_AUTH_ERROR(-211, "未登录"),
            return R.error(ResponseEnum.LOGIN_AUTH_ERROR.getMessage());
        }
    }

    @ApiOperation("登录记录日志列表")
    @PostMapping("/getUserLoginRecord")
    public R getUserLoginRecord(HttpServletRequest request) {
        List<UserLoginRecord> recordList = userLoginRecordService.listTop50();
        return R.ok().put("list", recordList);
    }
}
